using System.Drawing;
using System.Windows.Forms;
using Tiwo.Design.ItemRenderers;
using Tiwo.Design.Renderers;

namespace Tiwo.Design.Trackers
{
    public class ResizeRendererMouseTracker : IMouseTracker
    {
        public static readonly SizeF MinimumSize = new SizeF(10, 10);

        private PointF DragStartPoint { get; set; }
        public ResizeDirection Direction { get; set; }

        public RectangularRenderer Renderer { get; set; }
        private RectangleF RendererRect { get; set; }

        public SchemeRenderSite Site { get; set; }
        public Cursor Cursor
        {
            get
            {
                switch (Direction)
                {
                case ResizeDirection.Top:
                case ResizeDirection.Bottom:
                    return Cursors.SizeNS;
                case ResizeDirection.Left:
                case ResizeDirection.Right:
                    return Cursors.SizeWE;
                case ResizeDirection.Top | ResizeDirection.Left:
                case ResizeDirection.Bottom | ResizeDirection.Right:
                    return Cursors.SizeNWSE;
                case ResizeDirection.Bottom | ResizeDirection.Left:
                case ResizeDirection.Top | ResizeDirection.Right:
                    return Cursors.SizeNESW;
                }
                return Cursors.Default;
            }
        }

        public ResizeRendererMouseTracker(RectangularRenderer renderer, ResizeDirection direction)
        {
            Renderer = renderer;
            Direction = direction;
        }

        public void MouseDown(Point location)
        {
            DragStartPoint = location;
            RendererRect = new RectangleF(Renderer.Location, Renderer.Size);
        }

        public void MouseMove(Point location)
        {
            if (DragStartPoint.IsEmpty) return;

            var dx = location.X - DragStartPoint.X;
            var dy = location.Y - DragStartPoint.Y;

            if (Direction.IsVertical()) dx = 0;
            if (Direction.IsHorizontal()) dy = 0;
            if (Direction.Has(ResizeDirection.Left)) dx = -dx;
            if (Direction.Has(ResizeDirection.Top)) dy = -dy;

            var size = RendererRect.Size;
            size.Width += dx;
            size.Height += dy;

            if (size.Width <= MinimumSize.Width || size.Height <= MinimumSize.Height)
                return;

            if (!Direction.Has(ResizeDirection.Left)) dx = 0;
            if (!Direction.Has(ResizeDirection.Top)) dy = 0;

            var pt = RendererRect.Location;
            pt.X -= dx;
            pt.Y -= dy;

            Renderer.Location = pt;
            Renderer.Size = size;

            Site.Invalidate();
        }

        public void MouseUp(Point location) { }

        public void DrawTo(Graphics graphics, IRenderingCache rendering)
        {
            var areas = ResizeRectangularModificator.CreateResizeAreas(Renderer);

            graphics.DrawRectangle(Pens.LightBlue, Renderer.Location.X, Renderer.Location.Y, Renderer.Size.Width, Renderer.Size.Height);
            foreach (var area in areas)
            {
                graphics.DrawRectangle(Pens.Black, area.Value.X, area.Value.Y, area.Value.Width, area.Value.Height);
            }
        }
    }
}